package com.mgface.framework.configuration;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import com.mgface.framework.dao.BasedDao;

@Configuration//标记此类作为ApplicationContext的beans定义的源
@Order(2)
public class GenericDaoConfiguration {
	@Bean(name = "intBean")
	public BasedDao<Integer> createIntegerDao() {
		return new BasedDao<Integer>() {
			@Override
			public String getGenericName() {
				Class<?> clazz = this.getClass();
				Type t = clazz.getGenericInterfaces()[0];
				if (t instanceof ParameterizedType) {
					Type[] args = ((ParameterizedType) t).getActualTypeArguments();
					if (args[0] instanceof Class) {
						clazz = (Class<?>) args[0];
					}
				}
				return clazz.getSimpleName();
			}
		};
	}

	@Bean(name = "stringBean")
	public BasedDao<String> createStringDao() {
		return new BasedDao<String>() {
			@Override
			public String getGenericName() {
				Class<?> clazz = this.getClass();
				Type t = clazz.getGenericInterfaces()[0];
				if (t instanceof ParameterizedType) {
					Type[] args = ((ParameterizedType) t).getActualTypeArguments();
					if (args[0] instanceof Class) {
						clazz = (Class<?>) args[0];
					}
				}
				return clazz.getSimpleName();
			}
		};
	}
}
